<?php
defined('IN_MAIN') or exit('Invalid Access!');
        
function encrypt_user_login_sess_cookie($in_username, $in_password) {
/*
This function will encrypt the user login info (username & password) and store in both the user local cookie & server session information

in_username - the user login username, input as unencrypted
in_password - the user login password, input as encrypted: md5 encryption of password and salt
*/
global $vars;
//use a random salt for each login
$field_cookie_salt = md5("user_cookie_salt".SECRET_KEY);
if(isset($_COOKIE[$field_cookie_salt])){
 $cookie_salt = $_COOKIE[$field_cookie_salt];
}else{
 $cookie_salt = generate_random_code(32);
}
$enc_username=md5($in_username.$cookie_salt.SECRET_KEY);
$enc_password=md5($in_password.$cookie_salt.SECRET_KEY);
$field_username = md5("u_u".$cookie_salt.SECRET_KEY);
$field_password = md5("u_p".$cookie_salt.SECRET_KEY);
// set cookie with value and set session with the same value.
setcookie ($field_cookie_salt, $cookie_salt, time()+$vars["admin_login_expire"], WORK_DIR.'/members/');
setcookie ($field_username, $enc_username, time()+$vars["admin_login_expire"], WORK_DIR.'/members/');
setcookie ($field_password, $enc_password, time()+$vars["admin_login_expire"], WORK_DIR.'/members/');
ini_set("session.gc_maxlifetime", $vars["admin_login_expire"]);
$_SESSION["cookie_chk"][$field_cookie_salt] = $cookie_salt;
$_SESSION["cookie_chk"][$field_username] = $enc_username;
$_SESSION["cookie_chk"][$field_password] = $enc_password;
}

function check_user_is_logged(){
global $vars;
$db=$vars["db"];

$success = false;
$uid=$_SESSION["uid"];
if($uid){
 $field_cookie_salt = md5("user_cookie_salt".SECRET_KEY);
 if(isset($_SESSION['cookie_chk'][$field_cookie_salt])){
  $cookie_salt = $_SESSION['cookie_chk'][$field_cookie_salt];
  $field_username = md5("u_u".$cookie_salt.SECRET_KEY);
  $field_password = md5("u_p".$cookie_salt.SECRET_KEY);

  $r=mysql_query($sql="select username, enc_password from $db->users where id='$uid'");
  if(@mysql_num_rows($r)){
   $username=mysql_result($r, 0, "username");
   $password=mysql_result($r, 0, "enc_password");
   $enc_username=md5($username.$cookie_salt.SECRET_KEY);
   $enc_password = md5($password.$cookie_salt.SECRET_KEY);
   if(isset($_SESSION["cookie_chk"][$field_username]) && isset($_SESSION["cookie_chk"][$field_password])){
     if($_SESSION["cookie_chk"][$field_username] == $enc_username && $_SESSION["cookie_chk"][$field_password] == $enc_password){
      $success = true;
     }
   }
  }
 }
}

if($success){
 encrypt_user_login_sess_cookie($username, $password);
}
return $success;
}

function verify_user_sess_data(){
global $vars;
$db=$vars["db"];

$success = false;
$uid=$_SESSION["uid"];
if($uid){
 $field_cookie_salt = md5("user_cookie_salt".SECRET_KEY);
 if(isset($_COOKIE[$field_cookie_salt])){
  $cookie_salt = $_COOKIE[$field_cookie_salt];
  $field_username = md5("u_u".$cookie_salt.SECRET_KEY);
  $field_password = md5("u_p".$cookie_salt.SECRET_KEY);

  $r=mysql_query($sql="select username, enc_password from $db->users where id='$uid'");
  if(@mysql_num_rows($r)){
   $username=mysql_result($r, 0, "username");
   $password=mysql_result($r, 0, "enc_password");
   $enc_username=md5($username.$cookie_salt.SECRET_KEY);
   $enc_password = md5($password.$cookie_salt.SECRET_KEY);
   if(isset($_COOKIE[$field_username]) && isset($_COOKIE[$field_password]) && isset($_SESSION["cookie_chk"][$field_username]) && isset($_SESSION["cookie_chk"][$field_password])){
    if($_COOKIE[$field_username] == $_SESSION["cookie_chk"][$field_username] && $_COOKIE[$field_password] == $_SESSION["cookie_chk"][$field_password]){
     if($_SESSION["cookie_chk"][$field_username] == $enc_username && $_SESSION["cookie_chk"][$field_password] == $enc_password){
      $success = true;
     }
    }
   }
  }
 }
}

if($success){
 encrypt_user_login_sess_cookie($username, $password);
}else{
  if($_GET["aj"]){//this is an AJAX call
   print format_xml(array("loggedout"=>"loggedout"));
  }else{
   $return='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
   header("Location:". M_URL ."/".$vars["file"]["public"]["login"]."?logged-out&url=".urlencode($return));
  }
  exit();
}
}
?>